package com.wy.business.developer.apps.actions;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.developer.apps.services.AppSubmitService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;
import com.wy.component.dataConvert.BASE64Util;

/**
 * 项目名称：openportal
 * 类名称：AppSubmitAction
 * 类描述：APP提交上架
 * 创建人：张旭星
 * 创建时间：2016-3-22 下午3:11:09
 * 修改人：张旭星
 * 修改时间：2016-3-22 下午3:11:09
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/apps/appSubmit")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class AppSubmitAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(AppSubmitAction.class);

	@Autowired
	private AppSubmitService appSubmitService;
	
	private List<Map> rows;
	private List<Map> orgInfo;
	private String id;

	/**
	 * Function Name               list                                   
	 * @return
	 * @throws ServiceException          		             
	 * @description APP提交上架
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-4 下午1:46:43           张旭星                     Initial
	 **********************************************************************
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "saveApp", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String saveApp() throws ServiceException {
		String[] authRows = ServletActionContext.getRequest().getParameterValues("authRow");
		String[] imgShots = ServletActionContext.getRequest().getParameterValues("imgShot");
		String appIcon = ServletActionContext.getRequest().getParameter("appIcon");
		String functionName = ServletActionContext.getRequest().getParameter("functionName");
		String functionNameEn = ServletActionContext.getRequest().getParameter("functionNameEn");
		String deploymentType = ServletActionContext.getRequest().getParameter("deploymentType");
		String actionUrl = ServletActionContext.getRequest().getParameter("actionUrl");
		String appZipUrl = ServletActionContext.getRequest().getParameter("appZipUrl");
		String appSize = ServletActionContext.getRequest().getParameter("appSize");
		String versionName = ServletActionContext.getRequest().getParameter("versionName");
		String[] ipAddress = ServletActionContext.getRequest().getParameterValues("ipAddress");
		String[] ipAddressDesc = ServletActionContext.getRequest().getParameterValues("ipAddressDesc");
		String net_setting = ServletActionContext.getRequest().getParameter("net_setting");
//		String versionNo = ServletActionContext.getRequest().getParameter("versionNo");
		String manageUrl = ServletActionContext.getRequest().getParameter("manageUrl");
		String description = ServletActionContext.getRequest().getParameter("description");
		String upgradeContent = ServletActionContext.getRequest().getParameter("upgradeContent");
		String functionId = ServletActionContext.getRequest().getParameter("functionId");
		try {
			rspCode = appSubmitService.saveApp(imgShots,appIcon,functionName,functionNameEn,
					deploymentType,actionUrl,appZipUrl,appSize,versionName,ipAddress,ipAddressDesc,
					net_setting,manageUrl,description,upgradeContent,authRows,functionId);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               updateApp                                   
	 * @return
	 * @throws ServiceException          		             
	 * @description 编辑应用
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-3-26 下午2:13:58           张旭星                     Initial
	 **********************************************************************
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "updateApp", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
	"false"}) })
	public String updateApp() throws ServiceException {
		String[] authRows = ServletActionContext.getRequest().getParameterValues("authRow");
		String[] imgShots = ServletActionContext.getRequest().getParameterValues("imgShot");
		String appIcon = ServletActionContext.getRequest().getParameter("appIcon");
		String functionName = ServletActionContext.getRequest().getParameter("functionName");
		String functionNameEn = ServletActionContext.getRequest().getParameter("functionNameEn");
		String deploymentType = ServletActionContext.getRequest().getParameter("deploymentType");
		String actionUrl = ServletActionContext.getRequest().getParameter("actionUrl");
		String appZipUrl = ServletActionContext.getRequest().getParameter("appZipUrl");
		String appSize = ServletActionContext.getRequest().getParameter("appSize");
		String versionName = ServletActionContext.getRequest().getParameter("versionName");
		String[] ipAddress = ServletActionContext.getRequest().getParameterValues("ipAddress");
		String[] ipAddressDesc = ServletActionContext.getRequest().getParameterValues("ipAddressDesc");
		String net_setting = ServletActionContext.getRequest().getParameter("net_setting");
//		String versionNo = ServletActionContext.getRequest().getParameter("versionNo");
		String manageUrl = ServletActionContext.getRequest().getParameter("manageUrl");
		String description = ServletActionContext.getRequest().getParameter("description");
		String upgradeContent = ServletActionContext.getRequest().getParameter("upgradeContent");
		String functionId = ServletActionContext.getRequest().getParameter("functionId");
		try {
			rspCode = appSubmitService.updateApp(imgShots,appIcon,functionName,functionNameEn,
					deploymentType,actionUrl,appZipUrl,appSize,versionName,ipAddress,ipAddressDesc,
					net_setting,manageUrl,description,upgradeContent,authRows,functionId);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * Function Name               upGradeApp                                   
	 * @return
	 * @throws ServiceException          		             
	 * @description 升级APP
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-3-28 下午2:16:46           田松林                   Initial
	 **********************************************************************
	 */
	@Action(value = "upGradeApp", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
	"false"}) })
	public String upGradeApp() throws ServiceException {
		String[] authRows = ServletActionContext.getRequest().getParameterValues("authRow");
		String[] imgShots = ServletActionContext.getRequest().getParameterValues("imgShot");
		String appIcon = ServletActionContext.getRequest().getParameter("appIcon");
		String functionName = ServletActionContext.getRequest().getParameter("functionName");
		String functionNameEn = ServletActionContext.getRequest().getParameter("functionNameEn");
		String deploymentType = ServletActionContext.getRequest().getParameter("deploymentType");
		String actionUrl = ServletActionContext.getRequest().getParameter("actionUrl");
		String appZipUrl = ServletActionContext.getRequest().getParameter("appZipUrl");
		String appSize = ServletActionContext.getRequest().getParameter("appSize");
		String versionName = ServletActionContext.getRequest().getParameter("versionName");
		String[] ipAddress = ServletActionContext.getRequest().getParameterValues("ipAddress");
		String[] ipAddressDesc = ServletActionContext.getRequest().getParameterValues("ipAddressDesc");
		String net_setting = ServletActionContext.getRequest().getParameter("net_setting");
//		String versionNo = ServletActionContext.getRequest().getParameter("versionNo");
		String manageUrl = ServletActionContext.getRequest().getParameter("manageUrl");
		String description = ServletActionContext.getRequest().getParameter("description");
		String upgradeContent = ServletActionContext.getRequest().getParameter("upgradeContent");
		String functionId = ServletActionContext.getRequest().getParameter("functionId");
		try {
			rspCode = appSubmitService.updateUpgradeApp(imgShots,appIcon,functionName,functionNameEn,
					deploymentType,actionUrl,appZipUrl,appSize,versionName,ipAddress,ipAddressDesc,
					net_setting,manageUrl,description,upgradeContent,authRows,functionId);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * Function Name               list                                   
	 * @return
	 * @throws ServiceException          		             
	 * @description 我的APP编辑
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-4 下午1:46:43           张旭星                     Initial
	 **********************************************************************
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value = "fetchInfo", 
	interceptorRefs = { @InterceptorRef("sessionStack") }, 
	results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String fetchInfo() throws ServiceException {
		String functionId = ServletActionContext.getRequest().getParameter("functionId");
		try {
			rows = appSubmitService.getInfo(functionId);
		} catch (ErrorSqlException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public List<Map> getRows() {
		return rows;
	}

	public List<Map> getOrgInfo() {
		return orgInfo;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}

}
